package com.ancun;

import ancun.seal.servlet.SealClient;
import ancun.seal.util.FileUtil;
import ancun.seal.util.StringUtil;
import org.junit.Test;

import java.io.File;
import java.util.UUID;

/**
 * 普通制章
 * 
 */
public class FunctionPdfSealTest extends ServletBaseTest{
    @Test
    public void functionSealTest() throws Exception {
        /** HTTP方式 */
        String url = super.url + SealClient.SLASH + SealClient.PDF_SEAL_SERVLET;
        SealClient sealClient = new SealClient(url);

        //指定pfx路径
        String pfxPath = super.baseSources + "zjca1234.pfx";
//        byte[] pfx = FileUtil.getBytesFromFile(new File(pfxPath));
        byte[] pfx = "".getBytes();

        //指定pfx密码
        String pfxPasswd = "zjca1234";
//        String pfxPasswd = "";
        // 图片
        String imagePath = super.baseSources + "222.png";
        byte[] image = FileUtil.getBytesFromFile(new File(imagePath));

        String type = "2";      //2：坐标 3：关键字
        String sealPerson = "江苏江南农村商业银行股份有限公司";
        String sealLocation = "江苏省常州市天宁区和平中路 413 号";
        String sealResson = "签合同";

        // 支持（1）单页，（1,2,3,4）多页,（1-3）区间，（0）所有页
        String page = "1";

        String lX = "0.456942";
        String lY = "0.520723";

        // 签章错误
        String keyword = "杭州安存科技网络有限公司";//"投资合作协议";//"配送合作协议";
//        String keyword = "投资合作协议";

        String locationStyle = "";

        String offsetX = "0";
        String offsetY = "0";

//        String sealStrategyXML = "<Request>" + "<Type>" + type + "</Type>"
//                + "<Page>" + page + "</Page><SealPerson>" + sealPerson
//                + "</SealPerson><SealLocation>" + sealLocation + "</SealLocation>" + "<SealResson>" + sealResson + "</SealResson>" + "<LX>" + lX + "</LX><LY>" + lY
//                + "</LY><Keyword>" + keyword + "</Keyword>" + "<LocationStyle>" + locationStyle
//                + "</LocationStyle>" + "<OffsetX>" + offsetX + "</OffsetX><OffsetY>" + offsetY + "</OffsetY>" + "</Request>";

//        String sealStrategyXML = "<Request><Head/><Body><SerialVersionUID/>" + "<Type>" + type + "</Type>"
//                + "<Page>" + page + "</Page><SealPerson>" + sealPerson
//                + "</SealPerson><SealLocation>" + sealLocation + "</SealLocation>" + "<SealResson>" + sealResson + "</SealResson>" + "<LX>" + lX + "</LX><LY>" + lY
//                + "</LY><Keyword>" + keyword + "</Keyword>" + "<LocationStyle>" + locationStyle
//                + "</LocationStyle>" + "<OffsetX>" + offsetX + "</OffsetX><OffsetY>" + offsetY + "</OffsetY>" + "</Body></Request>";

        String sealStrategyXML = "<Request>\n" +
                "<Head/>\n" +
                "<Body>\n" +
                "<SerialVersionUID/>\n" +
                "<Type>" + type + "</Type>\n" +
                "<SealResson>" + sealResson + "</SealResson>\n" +
                "<Page>" + page + "</Page>\n" +
                "<Keyword>" + keyword + "</Keyword>\n" +
                "<LX>" + lX + "</LX>\n" +
                "<LY>" + lY + "</LY>\n" +
                "</Body>\n" +
                "</Request>";

//        StringBuilder batchSealStrategyXMLs = new StringBuilder();
//        batchSealStrategyXMLs.append("<List>");
//        int cnt = 3;
//        for (int i = 0; i < cnt; ++i)
//        {
//            batchSealStrategyXMLs.append(sealStrategyXML);
//            if (i == cnt - 1)
//            {
//                batchSealStrategyXMLs.append("</List>");
//            }
//            else
//            {
//                batchSealStrategyXMLs.append(",");
//            }
//        }
//        System.out.println(batchSealStrategyXMLs);

//        String path = super.baseSources + "合伙人-diffword50_adobe-mupdf-验章字节范围错误_已修复.pdf";
        String path = super.baseSources + "1612170866050e61fb.pdf";
//        String path = super.baseSources + "4.pdf";
        byte[] pdf = FileUtil.getBytesFromFile(new File(path));

        long time1 = System.currentTimeMillis();
        byte[] sealedPdf = sealClient.sealPdfFunction(pdf, pfx, pfxPasswd, image, sealStrategyXML);
        long time2 = System.currentTimeMillis();
        System.out.println("Time: " + (time2-time1));

        if (sealedPdf.length < 255) {
            System.out.println("签章路径: " + new String(sealedPdf));
        } else if (StringUtil.checkResultDataValid(sealedPdf)) {
            String savePath = super.targetSources + UUID.randomUUID() + ".pdf";
            FileUtil.wirteDataToFile(savePath, sealedPdf);
            System.out.println("用例4：10个不同的关键字签章：" + savePath + "\'");
        } else {
            System.out.println("batchSealAutoPdfTest(Servlet) wrong " + new String(sealedPdf, "UTF-8"));
        }
        System.out.println(sealedPdf.length);
    }

}
